﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using MyVocabulary.App_Code;

namespace MyVocabulary
{
    public partial class AddWord : Form
    {
        #region Form messages

        protected static class FormMessages
        {
            public const string ValidateWord = "Please enter a word";
            public const string ValidateTranslation = "Please enter a translation";
        }

        #endregion

        public enum AddWordWindowMode
        {
            Add,
            Edit
        }

        public AddWordWindowMode Mode { get; set; }

        public VocabularyWord VocabWord { get; set; }

        public event EventHandler OnDataSaved;

        public AddWord(AddWordWindowMode Mode, EventHandler OnDataSaved)
        {
            InitializeComponent();
            
            this.Mode = Mode;
            this.OnDataSaved = OnDataSaved;
            Text = Mode.ToString();
            VocabWord = new VocabularyWord();
        }

        public AddWord(AddWordWindowMode Mode, EventHandler OnDataSaved, VocabularyWord VocabWord)
        {
            InitializeComponent();

            this.Mode = Mode;
            this.OnDataSaved = OnDataSaved;
            Text = Mode.ToString();
            this.VocabWord = VocabWord;

            tbWord.Text = VocabWord.Word;
            tbTranslation.Text = VocabWord.Translation;
            cbPartOfSpeech.Text = VocabWord.PartOfSpeech;
            SetText(tbExamples, VocabWord.Examples);
            SetText(tbRemarks, VocabWord.Remarks);            
        }

        /// <summary>
        /// Fix multiline textboxes that do not show new lines
        /// </summary>
        /// <param name="tb"></param>
        /// <param name="text"></param>
        protected static void SetText(TextBox tb, string text)
        {
            if(tb.Multiline)
            {
                tb.Lines = (new Regex(@"\n|\r\n").Split(text));                
            }
            else
            {
                tb.Text = text;                
            }
        }

        /// <summary>
        /// Enable ctrl + A in TextBoxes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (!e.Control || e.KeyCode != Keys.A || !(sender is TextBox))
                return;

            var txt = (TextBox)sender;
            txt.SelectAll();
            e.Handled = true;
        }

        private void tbWord_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbWord.Text.Trim()))
                errorProvider1.SetError(tbWord, FormMessages.ValidateWord);
            else
                errorProvider1.SetError(tbWord, string.Empty);
        }

        private void tbTranslation_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbTranslation.Text.Trim()))
                errorProvider1.SetError(tbTranslation, FormMessages.ValidateTranslation);
            else
                errorProvider1.SetError(tbTranslation, string.Empty);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbWord.Text.Trim()) || string.IsNullOrEmpty(tbTranslation.Text.Trim()))
                return;

            VocabWord.Word = tbWord.Text;
            VocabWord.Translation = tbTranslation.Text;
            VocabWord.PartOfSpeech = cbPartOfSpeech.Text;
            VocabWord.Examples = tbExamples.Text;
            VocabWord.Remarks = tbRemarks.Text;

            if (OnDataSaved != null)
                OnDataSaved(this, null);

            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        #region Form Shortcuts

        private void AddWord_KeyDown(object sender, KeyEventArgs e)
        {
            if (!e.Control) return;

            switch (e.KeyCode)
            {
                case Keys.S:
                    e.Handled = true;
                    e.SuppressKeyPress = true;
                    btnOK_Click(sender, e);
                    break;
            }
        }

        #endregion        
    }
}
